import arcade

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
MOVE_SPEED = 3

class Ball:
    def __init__(self, x, y, vx, vy, radius, color):

        self.x = x
        self.y = y
        self.vx = vx  # x方向上的变化
        self.vy = vy  # y方向上的变化
        self.radius = radius
        self.color = color

    def draw(self):
        arcade.draw_circle_filled(self.x,
                                  self.y,
                                  self.radius,
                                  self.color)

    def update(self):
        """更新x, y的坐标值"""
        self.x += self.vx
        self.y += self.vy

        # 保证小球位置在窗口内
        if self.x < self.radius:
            self.x = self.radius

        if self.x > SCREEN_WIDTH - self.radius:
            self.x = SCREEN_WIDTH - self.radius

        if self.y < self.radius:
            self.y = self.radius

        if self.y > SCREEN_HEIGHT - self.radius:
            self.y = SCREEN_HEIGHT - self.radius


class MyGame(arcade.Window):

    def __init__(self, width, height, title):

        super().__init__(width, height, title)

        # 隐藏鼠标指针
        self.set_mouse_visible(False)

        arcade.set_background_color(arcade.color.ASH_GREY)

        self.ball = Ball(50, 50, 0, 0, 15, arcade.color.AUBURN)

    def on_draw(self):
        arcade.start_render()
        self.ball.draw()

    #def on_mouse_motion(self, x, y, dx, dy):
        # 将球的位置设置为鼠标的位置
    #   self.ball.x = x
    #    self.ball.y = y

    def update(self, delta_time):
        self.ball.update()

    def on_key_press(self, key, modifiers):
        """按下方向键, 小球向指定方向移动"""
        if key == arcade.key.LEFT:
            self.ball.vx = -MOVE_SPEED
        elif key == arcade.key.RIGHT:
            self.ball.vx = MOVE_SPEED
        elif key == arcade.key.UP:
            self.ball.vy = MOVE_SPEED
        elif key == arcade.key.DOWN:
            self.ball.vy = -MOVE_SPEED

    def on_key_release(self, key, modifiers):
        """松开按键时, 小球不再移动"""
        if key == arcade.key.LEFT or key == arcade.key.RIGHT:
            self.ball.vx = 0
        elif key == arcade.key.UP or key == arcade.key.DOWN:
            self.ball.vy = 0


def main():
    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing Example")
    arcade.run()

main()